﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Windows.Forms;
using System.ComponentModel;
using SpellCaster3.Core;
using Expand.Collections.Generic;
using Expand.Collections;

namespace SpellCaster3
{
    public static class SpellCasterManager
    {
        public static Profile CurrentProfile = new Profile();
        public static string CurrentProfilePath = string.Empty;

        public static string CurrentProfileName
        {
            get
            {
                if (string.IsNullOrWhiteSpace(CurrentProfilePath))
                    return "Nuovo profilo";
                else
                    return System.IO.Path.GetFileNameWithoutExtension(CurrentProfilePath);
            }
        }

        public static bool LoadMinimized { get; set; }

        public static void Save(string path)
        {
            using (StreamWriter writer = new StreamWriter(path, false, Encoding.UTF8))
            {
                XmlSerializer serializer = new XmlSerializer(CurrentProfile.GetType());
                serializer.Serialize(writer, CurrentProfile);
            }
            if (!string.IsNullOrWhiteSpace(path)) AddToRecentProfiles(path);
        }

        public static void Load(string path, bool loadMinimized = false)
        {
            if (LoadMinimized)
                LoadMinimized = false;
            else
                LoadMinimized = loadMinimized;

            if (!File.Exists(path)) throw new FileNotFoundException();

            Profile profile;
            using (StreamReader reader = new StreamReader(path, Encoding.UTF8))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Profile));
                profile = serializer.Deserialize(reader) as Profile;
            }

            CurrentProfile = profile;
            CurrentProfilePath = path;
            UndoSaveCommandsAmount = 0;
            if (!string.IsNullOrWhiteSpace(path)) AddToRecentProfiles(path);
        }

        private static void AddToRecentProfiles(string path)
        {
            if (SpellCaster3.Properties.Settings.Default.RecentProfiles == null) SpellCaster3.Properties.Settings.Default.RecentProfiles = new StringCollection();

            int index = SpellCaster3.Properties.Settings.Default.RecentProfiles.IndexOf(path);
            if (index >= 0)
                SpellCaster3.Properties.Settings.Default.RecentProfiles.Swap(index, 0);
            else
                SpellCaster3.Properties.Settings.Default.RecentProfiles.Insert(0, path);

            if (SpellCaster3.Properties.Settings.Default.RecentProfiles.Count > SpellCaster3.Properties.Settings.Default.MaxRecentProfiles)
                SpellCaster3.Properties.Settings.Default.RecentProfiles.RemoveAt(SpellCaster3.Properties.Settings.Default.RecentProfiles.Count - 1);

            SpellCaster3.Properties.Settings.Default.Save();

            OnRecentProfilesChanged(SpellCaster3.Properties.Settings.Default.RecentProfiles, EventArgs.Empty);
        }

        public static event EventHandler RecentProfilesChanged;

        private static void OnRecentProfilesChanged(object sender, EventArgs e)
        {
            if (RecentProfilesChanged != null) RecentProfilesChanged(sender, e);
        }

        internal static int UndoSaveCommandsAmount = 0;
    }
}
